#ifndef _TIMER_H_
#define _TIMER_H_

#include <pc.h>
#include "ints.h"

/** @defgroup timer Timer
 * @{
 *
 * Timer-Counter definitions
 */

/** @name Timer Address */
/*@{*/
#define TIMER_0      0x40 ///< Address of Timer0 divider.
#define TIMER_1      0x41 ///< Address of Timer1 divider.
#define TIMER_2      0x42 ///< Address of Timer2 divider.
#define TIMER_CTRL   0x43 ///< The address of the timer control register.
/*@}*/

/** @name Speaker and Timer2 control and bit meanings */
/*@{*/
#define SPEAKER_CTRL    0x61     ///< The address of a generic PC register, whose bits 0 and 1 control the timer2 clock and output to the speaker.
#define TIMER2_ENABLE  (1 << 0) ///< Enable Timer 2 clock.
#define SPEAKER_ENABLE (1 << 1) ///< Enable Timer 2 output to speaker.
/*@}*/

/** @name Some useful (and nice) definitions */
/*@{*/
#define TIMER_CLK    1193181 ///< The timers input frequency.
#define LCOM_MODE  0x36    ///< Control byte: LSB followed by MSB, mode 3, binary count.
#define LSB(i) ((i) & 0xFF)    ///< The Least Significative Byte of a Word.
#define MSB(i) ((i) >> 8)      ///< The Most Significative Byte of a Word.
/*@}*/



/** Variable used for counting miliseconds (not defined here).
*/
extern volatile int msec;

/** Variable used for counting seconds (not defined here).
*/
extern volatile int sec;

void asm_wait(void);



/** Program timer (0,1,2) with mode.
 */
void timer_init(int timer, int mode);


/** Load timer (0,1,2) with value.
 */
void timer_load(int timer, int value);


/** Get divider for obtaining "freq" frequency.
*/
int get_divider(float freq);


/** Wait mili miliseconds, indirectly counting T0 or RTC interrupts.
*/
void mili_sleep(int mili);


/** wait secs seconds, indirectly counting T0 or RTC interrupts.
 */
void sec_sleep(int secs);


/** @} end of timer */

#endif
